﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class caserioController : Controller
    {
        public desarrolloganaderoEntities entx = new desarrolloganaderoEntities();
        //
        // GET: /caserio/
        public ActionResult IndexById(int id)
        {

            var query = entx.caserios
                .Where(cs => cs.idcaserio == id);

            var result = (from c in query.ToList()
                          join cp in entx.centropobladoes on c.idcentropoblado equals cp.idcentropoblado
                          join d in entx.distritoes on cp.iddistrito equals d.iddistrito
                          select new
                          {
                              Id = c.idcaserio,
                              Caserio = c.caserio1,
                              Centropoblado = cp.centropoblado1,
                              Distrito = d.distrito1,
                          }).ToList();
            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {

            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);
            string caseriox = Convert.ToString(Request.QueryString["query"]);

            var query = entx.caserios
                .Where(cs => cs.idcaserio>=1);

            if (!String.IsNullOrEmpty(caseriox)) { 
                query = query.Where(cs => cs.caserio1.ToUpper().Contains(caseriox.ToUpper())); }

            var result = (from c in query.OrderBy(cs => cs.caserio1).Skip(start).Take(limit).ToList()
                          join cp in entx.centropobladoes on c.idcentropoblado equals cp.idcentropoblado
                          join d in entx.distritoes on cp.iddistrito equals d.iddistrito
                          select new
                          {
                              Id = c.idcaserio,
                              Caserio = c.caserio1,
                              Centropoblado = cp.centropoblado1,
                              Distrito = d.distrito1,
                          }).ToList();
            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /caserio/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /caserio/Create

        //public ActionResult Create()
        //{
        //    return View();
        //} 

        //
        // POST: /caserio/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /caserio/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /caserio/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /caserio/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /caserio/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
